package com.mk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mk.constant.MessageConstant;
import com.mk.dto.OfficerLoginDTO;
import com.mk.entity.Officer;
import com.mk.exception.AccountNotFoundException;
import com.mk.exception.PasswordErrorException;
import com.mk.mapper.OfficerMapper;
import com.mk.service.IOfficerService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * 客户端执法人员业务接口实现类
 */
@Service
@RequiredArgsConstructor
public class OfficerServiceImpl extends ServiceImpl<OfficerMapper, Officer> implements IOfficerService {

    private final OfficerMapper officerMapper;

    /**
     * 执法人员登录
     * @param officerLoginDTO
     * @return
     */
    @Override
    public Officer login(OfficerLoginDTO officerLoginDTO) {
        String username = officerLoginDTO.getUsername();
        String password = officerLoginDTO.getPassword();

        Officer officer = officerMapper.selectOne(new LambdaQueryWrapper<Officer>()
                .eq(Officer::getUsername, username));

        if (officer == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

//        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(officer.getPassword())) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        return officer;
    }
}
